package com.shwread.android.qysw.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.shwread.android.qysw10000204.R;
import com.shwread.android.ui.widget.MyGoodsAllView;
import com.shwread.android.ui.widget.MyGoodsNoDeliverView;
import com.shwread.android.ui.widget.MyGoodsNoPayView;

import java.util.ArrayList;
import java.util.List;

import logic.util.IRefreshViewListener;


/**
 * 我的商品主页面
 * 
 * @author zhangnannan
 */

public class MyGoodsActivity
        extends
        BaseBusinessActivity
        implements
        OnClickListener
{
	private BaseBusinessActivity bActivity;
	private Context              context;
	private TextView             tvTitle;
	private Button               butBack;
	
	private TextView             tvTabAll, tvTabNoPay, TvTabNoDeliver, tvTabNoComment;
	
	private ViewPager            viewPager;
	private List<View>           lvViewPager;
	private ViewPagerAdapter     viewPagerAdapter;
	
	private final int            TAB_ALL            = 0;
	private final int            TAB_NO_PAY_TAG     = 1;
	private final int            TAB_NO_DELIVER_TAG = 2;
	private final int            TAB_NO_COMMENT_TAG = 3;
	private int                  currentPage        = 0;
	private Handler              handler            = new Handler()
	                                                {
		                                                
	                                                };
	
	@Override
	public Handler getUIHandler() {
		// TODO Auto-generated method stub
		return handler;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_goods);
		
		context = this;
		bActivity = this;
		findView();
		addListener();
		initData();
	}
	
	private void findView() {
		tvTitle = (TextView) findViewById(R.id.common_header_name);
		butBack = (Button) findViewById(R.id.common_header_back);
		tvTitle.setText("我的商品");
		butBack.setVisibility(View.VISIBLE);
		
		tvTabAll = (TextView) findViewById(R.id.my_goods_tab_all);
		tvTabAll.setSelected(true);
		tvTabNoPay = (TextView) findViewById(R.id.my_goods_tab_no_pay);
		TvTabNoDeliver = (TextView) findViewById(R.id.my_goods_tab_no_deliver);
		tvTabNoComment = (TextView) findViewById(R.id.my_goods_tab_no_comment);
		tvTabNoComment.setVisibility(View.GONE);
		
		viewPager = (ViewPager) findViewById(R.id.my_goods_view);
	}
	
	private void initData() {
		MyGoodsAllView myGoodsAllView = new MyGoodsAllView(context);
		MyGoodsNoPayView myGoodsNoPayView = new MyGoodsNoPayView(context);
		MyGoodsNoDeliverView myGoodsNoDeliverView = new MyGoodsNoDeliverView(context);
		//		MyGoodsNoCommentView myGoodsNoCommentView = new MyGoodsNoCommentView(context);
		
		lvViewPager = new ArrayList<View>();
		lvViewPager.add(myGoodsAllView);
		lvViewPager.add(myGoodsNoPayView);
		lvViewPager.add(myGoodsNoDeliverView);
		//		lvViewPager.add(myGoodsNoCommentView);
		viewPagerAdapter = new ViewPagerAdapter();
		viewPager.setAdapter(viewPagerAdapter);
		viewPager.setCurrentItem(TAB_ALL);
		
	}
	
	private void addListener() {
		butBack.setOnClickListener(this);
		tvTabAll.setOnClickListener(this);
		tvTabNoPay.setOnClickListener(this);
		TvTabNoDeliver.setOnClickListener(this);
		tvTabNoComment.setOnClickListener(this);
		viewPager.setOnPageChangeListener(new OnPageChangeListener()
		{
			
			@Override
			public void onPageSelected(int arg0) {
				setSelect(arg0);
				((IRefreshViewListener)(lvViewPager.get(arg0))).refreshView();
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_header_back:
//				BaseSimpleActivity.gotoMainActivity();
				finish();
				break;
			case R.id.my_goods_tab_all:
				setSelect(TAB_ALL);
				viewPager.setCurrentItem(TAB_ALL);
				break;
			case R.id.my_goods_tab_no_pay:
				setSelect(TAB_NO_PAY_TAG);
				viewPager.setCurrentItem(TAB_NO_PAY_TAG);
				break;
			case R.id.my_goods_tab_no_deliver:
				setSelect(TAB_NO_DELIVER_TAG);
				viewPager.setCurrentItem(TAB_NO_DELIVER_TAG);
				break;
			case R.id.my_goods_tab_no_comment:
				setSelect(TAB_NO_COMMENT_TAG);
				viewPager.setCurrentItem(TAB_NO_COMMENT_TAG);
				break;
			default:
				break;
		}
		
	}
	
	private void setSelect(int a) {
		currentPage = a;
		tvTabAll.setSelected(a == TAB_ALL ? true : false);
		tvTabNoPay.setSelected(a == TAB_NO_PAY_TAG ? true : false);
		TvTabNoDeliver.setSelected(a == TAB_NO_DELIVER_TAG ? true : false);
		tvTabNoComment.setSelected(a == TAB_NO_COMMENT_TAG ? true : false);
	}
	@Override
	protected void onResume() {
	    super.onResume();
	    ((IRefreshViewListener)(lvViewPager.get(currentPage))).refreshView();
//	    ((IRefreshViewListener)(lvViewPager.get(viewPager.getCurrentItem()))).refreshView();
	}
	class ViewPagerAdapter
	        extends
	        PagerAdapter
	{
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return lvViewPager.size();
		}
		
		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}
		
		@Override
		public Object instantiateItem(View container, int position) {
			((ViewPager) container).addView(lvViewPager.get(position));
			return lvViewPager.get(position);
		}
		
		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager) container).removeView(lvViewPager.get(position));
			
		}
		
	}
	
}
